<?php
App::import ( 'Vendor', 'weibo/weibooauth' );

class NotesController extends AppController {

	var $name = 'Notes';
	var $scaffold='admin';
	
		public $components = array('Session',
    'Auth'=> array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email','password'=>'password')
            )
        )
    )
);
	
	function page($id, $offset){
		$this->layout = "min";
		
		$notes = $this->Note->find('all',array(
			'fields'=>array('Note.timestamp','Note.content','Note.status','user.name','user.remoteid','user.avatar_url'), 
			'conditions'=>array('Note.book'=>$id),
			'order'=>array('Note.id DESC'), 
			'limit'=>10, 
			'offset'=>$offset));
		
		echo json_encode($notes);
	}
	
	function counts($sids){
		$this->layout = "min";
		$last_key = $this->Session->read ( 'token' );
		$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
		$counts = $c->get_count_info_by_ids($sids);
		echo json_encode($counts);
	}

	function add($id){
		$this->layout = "min";
		$this->loadModel('Book');
		$this->loadModel('Stat');
		
		$user = $this->Auth->user();
		if( !isset($user)){
			echo json_encode(array('success'=>false,'msg'=>'NO_LOGIN'));
			return;
		}
		
		$book = $this->Book->find ( 'first', array ('conditions' => array ('Book.id' => $id ) ) );
		if( !isset($book)){
			echo json_encode(array('success'=>false,'msg'=>'NO_BOOK'));
			return;
		}
		
		$content =  $_REQUEST['content'];
		if( strlen($content) == 0){
			echo json_encode(array('success'=>false,'msg'=>'NO_CONTENT'));
			return;
		}
		
		
		$token = $this->Session->read ( 'token' );
		$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token['access_token'] );
		
		if( isset($_REQUEST['include_url'])){
			$status = $c->upload ( $content. ' http://book.lupinlin.com/books/view/'.$id, 'http://book.lupinlin.com/img/pic/' . $book ['Book'] ['id'] . '_300x420_0.jpg' );
		}else
			$status = $c->update ( $content );	
			
		$note['Note']['book'] = $id;
		$note['Note']['user'] = $user['user']['id'];
		$note['Note']['timestamp'] = date('Y-m-d H:i:s');
		$note['Note']['content'] =$content;
		$note['Note']['status'] = $status['id'];
		
		if( !isset($status['id'])){
			echo json_encode(array('success'=>false,'msg'=>'WEIBO_ERROR'));
			return;
		}
		
		$this->Note->save($note);
		
		$stat = $this->Stat->findByBook($id);
		if( !isset($stat)||$stat == false){
			$stat['Stat']['book'] = $id;
			$stat['Stat']['note_count'] = 1;
			$this->Stat->save($stat);
		}else{
			$this->Stat->id = $stat['Stat']['id'];
			$this->Stat->saveField('note_count',$stat['Stat']['note_count']+1);
		}
		
		$note = $this->Note->find('first',array(
			'conditions'=>'Note.id='.$this->Note->id,
			'fields'=>array('Note.timestamp','Note.content','Note.status','user.name','user.remoteid','user.avatar_url'),
			'order'=>'Note.id DESC'	
		));
		echo json_encode(array('success'=>true,'note'=>$note) );
	}
}
?>